package com.auction.controller.admin;

import com.auction.dto.AdminLoginDTO;
import com.auction.dto.UserDTO;
import com.auction.dto.UserPageQueryDTO;
import com.auction.pojo.Admin;
import com.auction.pojo.PageResult;
import com.auction.pojo.Result;
import com.auction.properties.JwtProperties;
import com.auction.service.AdminService;
import com.auction.service.UserService;
import com.auction.utils.BaseContext;
import com.auction.utils.JwtUtil;
import com.auction.vo.AdminLoginVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 管理员控制器
 */
@RestController
@RequestMapping("/admin")
@Slf4j
public class AdminController {

    @Autowired
    private AdminService adminService;
    @Autowired
    private JwtProperties jwtProperties;
    @Autowired
    private UserService userService;

    /**
     * 管理员登录
     * 
     * @param adminLoginDTO 登录信息
     * @return 登录结果
     */
    @PostMapping("/login")
    public Result<AdminLoginVO> login(@RequestBody AdminLoginDTO adminLoginDTO) {
        log.info("管理员登录：{}", adminLoginDTO);

        Admin admin = adminService.login(adminLoginDTO);

        // 登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put("adminId", admin.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        AdminLoginVO adminLoginVO = AdminLoginVO.builder()
                .id(admin.getId())
                .userName(admin.getUsername())
                .token(token)
                .build();

        return Result.success(adminLoginVO);
    }

    /**
     * 获取当前登录管理员信息
     * 
     * @return
     */
    @GetMapping("/info")
    public Result<Admin> info() {
        Long adminId = BaseContext.getCurrentId();
        Admin admin = adminService.getById(adminId);
        return Result.success(admin);
    }

    /**
     * 新增用户
     *
     * @param userDTO 用户信息
     * @return 新增结果
     */
    @PostMapping("/users")
    public Result saveUser(@RequestBody UserDTO userDTO) {
        log.info("新增用户：{}", userDTO);
        userService.register(userDTO);
        return Result.success();
    }

    /**
     * 分页查询用户列表
     * 
     * @param userPageQueryDTO
     * @return
     */
    @GetMapping("/users/page")
    public Result<PageResult> page(UserPageQueryDTO userPageQueryDTO) {
        log.info("分页查询用户列表：参数为{}", userPageQueryDTO);
        PageResult pageResult = userService.pageQueryUser(userPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 启用禁用用户账号
     * 
     * @param id     用户ID
     * @param status 用户状态（0-禁用，1-启用）
     * @return 更新结果
     */
    @PostMapping("/users/{status}")
    public Result startOrStop(@PathVariable Integer status, Long id) {
        log.info("启用禁用用户账号：{},{}", status, id);
        userService.startOrStop(status, id);
        return Result.success();
    }

}